import 'dart:io';

import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';

final List<File> fileList = [];

class FileWidget extends StatefulWidget {
  const FileWidget({super.key});

  @override
  State<StatefulWidget> createState() {
    return _FileWidget();
  }
}

class _FileWidget extends State<FileWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: const Text("文件"),
        centerTitle: true,
        toolbarHeight: 50,
        backgroundColor: Colors.white,
        titleTextStyle: const TextStyle(color: Colors.blue),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          TextButton(
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              foregroundColor: MaterialStateProperty.resolveWith((states) {
                if (states.contains(MaterialState.focused)) {
                  return Colors.white;
                } else if (states.contains(MaterialState.pressed)) {
                  return Colors.red;
                }
              }),
            ),
            onPressed: () {
              singleFile();
            },
            child: const Text("选择文件--单选"),
          ),
          TextButton(
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all<Color>(Colors.blue),
              foregroundColor: MaterialStateProperty.resolveWith((states) {
                if (states.contains(MaterialState.focused)) {
                  return Colors.white;
                } else if (states.contains(MaterialState.pressed)) {
                  return Colors.red;
                }
              }),
            ),
            onPressed: () {
              multiFile();
            },
            child: const Text("选择照片--多选"),
          ),
          getList(),
        ],
      ),
    );
  }

  Widget getList() {
    return Expanded(
      child: ListView.builder(
        shrinkWrap: false,
        // physics: const NeverScrollableScrollPhysics(),
        itemCount: fileList.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            child: Text(fileList[index].path,style: const TextStyle(color: Colors.blue),),
          );
        },
      ),
    );
  }

  singleFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles();
    fileList.clear();
    setState(() {
      if (result != null) {
        File file = File(result.files.single.path!);
        fileList.add(file);
      }
    });
  }

  multiFile() async {
    FilePickerResult? result =
        await FilePicker.platform.pickFiles(allowMultiple: true);
    fileList.clear();
    setState(() {
      if (result != null) {
        List<File> files = result.paths.map((path) => File(path!)).toList();
        fileList.addAll(files);
      }
    });
  }

  // 验证是否为null 或者 ''
  static bool validationInput(String? inputText) {
    return inputText?.isNotEmpty ?? false;
  }
}
